# -*- codeing = utf-8 -*-
# @Time : 2021/2/24 22:43
# @Atuthor: 朱朱
# @File: 081reduce方法的使用.py
# @Software: PyCharm
""
"""
对一个序列进行压缩运算，得到一个值。python3以后，这个方法被移到了functools模块
函数参数用来指定元素按照哪种方式合并
"""

from functools import reduce
# reduce 以前是一个内置函数
# 内置函数和内置类都在 builtins.py文件里

def foo(x,y):
    return x+y

scores = [100,89,76,87]
print(reduce(foo,scores))

students = [
    {'name': 'zhangsan', 'age': 18, 'score': 98, 'height': 180},
    {'name': 'lisi', 'age': 21, 'score': 97, 'height': 185},
    {'name': 'jack', 'age': 22, 'score': 100, 'height': 175},
    {'name': 'tony', 'age': 23, 'score': 90, 'height': 176},
    {'name': 'henry', 'age': 20, 'score': 95, 'height': 172}
]

# def bar(x, y):
#     # x= 0
#     # y = {'name': 'zhangsan', 'age': 18, 'score': 98, 'height': 180},
#     return x + y['age']


# reduce(函数,序列,初始值)
# print(reduce(bar, students, 0))
print(reduce(lambda x, y: x + y['age'], students, 0))